﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI;
using AcctrueWMS.Foundation.Data;
using AcctrueWMS.Foundation.Web.Cache;
using System.Collections;
using System.Web;
using AcctrueWMS.Foundation.Web.Permission;

namespace AcctrueWMS.Foundation.Web
{
    using AcctrueWMS.Foundation.Web.Cache;
    using AcctrueWMS.Foundation.Web.Config;
    using AcctrueWMS.Foundation.Web.Modules;
    using AcctrueWMS.Foundation.Web.Permission;
    using AcctrueWMS.Foundation.Web.Session;

    public static class Runtime
    {
        #region 获取SysFile Model Template目录路径

        private static string sysFilePath;
        public static string SysFilePath
        {
            get
            {
                if (sysFilePath == null)
                {
                    lock (objhelper)
                    {
                        sysFilePath = HttpRuntime.AppDomainAppPath + "SysFiles";
                    }
                }
                return sysFilePath;
            }
        }
        private static string templateFilePath;
        public static string TemplateFilePath
        {
            get
            {
                if (templateFilePath == null)
                {
                    lock (objhelper)
                    {
                        templateFilePath = SysFilePath + "\\Templates";
                    }

                }
                return templateFilePath;
            }
        }

        private static string modelFilePath;
        public static string ModelFilePath
        {
            get
            {
                if (modelFilePath == null)
                {
                    lock (objhelper)
                    {
                        modelFilePath = SysFilePath + "\\Models";

                    }
                }
                return modelFilePath;
            }
        }

        private static string applicationPath;
        public static string ApplicationPath
        {
            get
            {
                if (applicationPath == null)
                {
                    lock (objhelper)
                    {
                        applicationPath = HttpRuntime.AppDomainAppVirtualPath;
                        if (applicationPath.LastIndexOf('/') == applicationPath.Length - 1)
                            applicationPath = applicationPath.Remove(applicationPath.Length - 1);
                    }
                }
                return applicationPath;
            }
        }

        private static string appPhysicalPath;
        public static string AppPhysicalPath
        {
            get
            {
                if (appPhysicalPath == null)
                {
                    lock (objhelper)
                    {
                        appPhysicalPath = HttpRuntime.AppDomainAppPath;
                    }
                }
                return appPhysicalPath;
            }
        }
        #endregion

        #region 对文件的操作

        /// <summary>
        /// 检查XML文件路径是否存在，若不存在则创建
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public static bool IsExistPath(string path)
        {
            if (Directory.Exists(path))
                return true;
            if (Directory.CreateDirectory(path) != null)
                return true;
            return false;
        }
        /// <summary>
        /// 检查XML文件是否存在
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public static bool IsExistFile(string fileFullPath)
        {
            if (File.Exists(fileFullPath))
                return true;
            return false;
        }
        #endregion

        //static bool inited = false;
        //public static bool Inited
        //{
        //    get { return inited; }
        //    set { inited = value; }
        //}
        //public static void Init()
        //{
        //    if (!inited)
        //    {
        //        ClsAliasManager.LoadFromAliasFile(Runtime.SysFilePath + "\\Startup\\Alias.xml");
        //        //增加xmlcache
        //        CacheManager.LoadFromFile(Runtime.SysFilePath + "\\Startup\\Cache.xml");
        //        inited = true;
        //    }
        //}
        static IDataModelContext context;
        private static object objhelper = new object();
        /// <summary>
        /// 上下文
        /// </summary>
        public static IDataModelContext Context
        {
            get
            {
                if (context == null)
                {
                    lock (objhelper)
                    {
                        if (context == null)
                        {
                            context = new DataModelContext();
                            ///2011-6-10 操敏增加,如果获取的ModelFilePath ApplicationPath为空,则不赋值
                            try
                            {
                                if (!string.IsNullOrEmpty(ModelFilePath))
                                    context.FilePath = ModelFilePath;
                                context.CommmonServiceUrl = ApplicationPath + "/Service/CommonService.asmx";
                                context.ApplicationPath = ApplicationPath;
                                //context.Render = new Render.VTLModelRender();
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                return context;
            }
        }
        public static Common.ICache Cache
        {
            get { return new Common.Cache(); }
        }
        static PermissionManager permissionManager;
        public static PermissionManager PermissionManager
        {
            get
            {
                if (permissionManager == null)
                {
                    lock (objhelper)
                    {
                        permissionManager = new PermissionManager();
                    }
                }
                return permissionManager;
            }
        }
        static ISessionManager sessionManager;
        public static ISessionManager SessionManager
        {
            get
            {
                if (sessionManager == null)
                {
                    lock (objhelper)
                    {
                        if (sessionManager == null)
                        {
                            sessionManager = new SessionManager();
                        }
                    }
                }
                return sessionManager;
            }
        }

        static ICacheManager cacheManager;
        public static ICacheManager CacheManager
        {
            get
            {
                if (cacheManager == null)
                {
                    lock (objhelper)
                    {
                        if (cacheManager == null)
                        {
                            cacheManager = new CacheManager();
                        }
                    }
                }
                return cacheManager;
            }
        }
        static IClsAliasManager clsAliasManager;
        /// <summary>
        /// 别名管理
        /// </summary>
        public static IClsAliasManager ClsAliasManager
        {
            get
            {
                if (clsAliasManager == null)
                {
                    lock (objhelper)
                    {
                        if (clsAliasManager == null)
                        {
                            clsAliasManager = new ClsAliasManager();
                        }
                    }
                }
                return clsAliasManager;
            }

        }

        static IConfigManager configManager;
        /// <summary>
        /// 别名管理
        /// </summary>
        public static IConfigManager ConfigManager
        {
            get
            {
                if (configManager == null)
                {
                    lock (objhelper)
                    {
                        if (configManager == null)
                        {
                            configManager = new Config.ConfigManager();
                        }
                    }
                }
                return configManager;
            }

        }
        public static SystemUser CurrentUser
        {
            get
            {
                return HttpContext.Current.Session[SessionConst.CurrentUser] as SystemUser;
            }
        }
        //static ModuleAuthorityManager moduleAuthorityManager;
        public static ModuleAuthorityManager ModuleAuthorityManager
        {
            get
            {
                string key = "moduleAuth_" + CurrentUser.UserNo;
                if (HttpContext.Current.Session[key] == null)
                {
                    lock (objhelper)
                    {
                        ModuleAuthorityManager moduleAuthorityManager = new ModuleAuthorityManager();
                        moduleAuthorityManager.InitModuleAuthority();
                        HttpContext.Current.Session[key] = moduleAuthorityManager;
                    }
                }
                return HttpContext.Current.Session[key] as ModuleAuthorityManager;
            }
        }

        private static Modules.ModuleManager moduleManager = null;
        public static Modules.ModuleManager ModuleManager
        {
            get
            {
                if (moduleManager == null)
                {
                    lock (objhelper)
                    {
                        if (moduleManager == null)
                        {
                            moduleManager = new Modules.ModuleManager();
                            moduleManager.LoadModules(SysFilePath + "/StartUp/Modules.xml");
                        }
                    }
                }
                return moduleManager;
            }
        }

        /// <summary>
        /// 获取指定目录下的指定类型文件
        /// </summary>
        /// <param name="path">文件目录</param>
        /// <param name="fileType">文件类型</param>
        /// <returns></returns>
        public static ArrayList GetFilePathByType(string path, string fileType)
        {
            ArrayList resArray = new ArrayList();
            string[] files = Directory.GetFiles(path, fileType);
            for (int i = 0; i < files.Length; i++)
            {
                resArray.Add(files[i]);
            }
            return resArray;
        }

        public static void ProcessRequest(HttpContext httpContext)
        {
            WebPage page = new WebPage(Context);
            page.Render(httpContext);
            string aa ;
            aa = "222";

        }



    }
}
